from flask import Blueprint, jsonify, current_app
from datetime import datetime

class Plugin:
    def __init__(self):
        self.bp = Blueprint('hello_world', __name__)
        self.enabled = True
        self.setup_routes()

    def setup_routes(self):
        """设置路由"""
        @self.bp.route('/greet', methods=['GET'])
        def greet():
            if not self.enabled:
                return jsonify({
                    'status': 'error',
                    'message': 'Plugin is disabled'
                }), 403

            config = current_app.plugin_manager.get_plugin_config('hello_world')
            response = {
                'message': config.get('message', 'Hello, World!'),
                'color': config.get('color', '#007bff')
            }
            
            if config.get('show_time', True):
                response['time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            
            return jsonify({
                'status': 'success',
                'data': response
            })

    def init(self, app):
        """初始化插件"""
        # 注册路由
        app.register_blueprint(self.bp, url_prefix='/api/plugins/hello_world')
        print("Hello World plugin initialized")

    def destroy(self):
        """销毁插件"""
        print("Hello World plugin destroyed")

    def enable(self):
        """启用插件"""
        self.enabled = True
        print("Hello World plugin enabled")

    def disable(self):
        """禁用插件"""
        self.enabled = False
        print("Hello World plugin disabled") 